package top.honlnk.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import top.honlnk.common.constant.AdminConstant;
import top.honlnk.common.exception.CustomException;
import top.honlnk.pojo.dto.AdminGetKeyDTO;
import top.honlnk.pojo.dto.AdminUpdateRotaDTO;
import top.honlnk.pojo.vo.AdminGetKeyVO;
import top.honlnk.pojo.vo.AdminGetRotaListVO;
import top.honlnk.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.honlnk.pojo.vo.AdminGetUserTableVO;
import top.honlnk.service.AdminService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
public class adminController {

    @Autowired
    private AdminService adminService;

    @PutMapping("/get_key")
    public Result<AdminGetKeyVO> getKey(@RequestBody AdminGetKeyDTO adminGetKeyDTO){
        String AdminKey = adminService.produceKey(adminGetKeyDTO);

        AdminGetKeyVO adminGetKeyVO = AdminGetKeyVO.builder()
                .Key(AdminKey)
                .UserKeyLink(AdminConstant.UPLODE_ACCESS_ADDRESS + AdminKey.substring(0, 11))
                .build();
        return Result.success(adminGetKeyVO);
    }

    @PutMapping ("/update_rota")
    public Result<String> updateRota(@RequestBody AdminUpdateRotaDTO adminUpdateRotaDTO, HttpServletRequest request){
        if (adminUpdateRotaDTO.getRota() == null)
            throw new CustomException(AdminConstant.ROTA_IS_NULL);
        adminService.updateRota(adminUpdateRotaDTO, request.getHeader("key"));
        return Result.success();
    }
    @PostMapping("/produce_rota")
    public Result<AdminGetRotaListVO> produceRota(HttpServletRequest request){
        String key = request.getHeader("key");
        AdminGetRotaListVO adminGetRotaListVO = adminService.produceRota(key);
        return Result.success(adminGetRotaListVO);
    }

    @GetMapping("/get_user_table")
    public Result<List<AdminGetUserTableVO>> getUserTable(HttpServletRequest request){
        String key = request.getHeader("key");
        List<AdminGetUserTableVO> schoolTimetableList = adminService.getUserTable(key);
        return Result.success(schoolTimetableList);
    }

    @DeleteMapping("/del_user_table")
    public Result<String> delUserTable(HttpServletRequest request){
        String submitId = request.getHeader("submitId");
        adminService.deleteUserTable(submitId);
        return Result.success();
    }

}
